/*
 *  Author: zhaijl 
 *  Url: http://218.1.73.212/down.php
 *  Country: China
 *  
 * ====================================================================
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This software based apache mina 2.0 and spring 3.0
 *
 */
package org.cloudy.http.mina;

import java.net.InetSocketAddress;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;

import org.cloudy.common.spring.SpringContextUtil;
import org.cloudy.common.thread.DefaultSync;
import org.cloudy.common.thread.Sync;
import org.cloudy.http.codec.CodecFactory;
import org.cloudy.http.conf.ContextLoader;

public class SimpleLoader {
	static Logger logger = Logger.getLogger ( SimpleLoader.class ) ;
	public static final String PASSWORD = "org.cloudy.password" ;
	private SocketAcceptor acceptor ;
	private IoHandler hander ;
	private IoFilter filter ;
	private boolean _flag = false ;
	private Sync _sync = new DefaultSync () ;	
	
	public static SimpleLoader getInstance () {
		return ( SimpleLoader ) SpringContextUtil.getBean ( "simpleLoader" ) ;
	}

	public void setAcceptor ( SocketAcceptor acceptor ) {
		this.acceptor = acceptor ;
	}

	public void setHander ( IoHandler hander ) {
		this.hander = hander ;
	}
	
	public void setFilter ( IoFilter filter ) {
		this.filter = filter ;
	}
	
	public SocketAcceptor getAcceptor () {
		return acceptor ;
	}

	public IoHandler getHander () {
		return hander ;
	}

	public IoFilter getFilter () {
		return filter;
	}
	
	public void serveToClient () {		
		try {	
			logger.info ( "server startup begin ... " ) ;			
			acceptor.setReuseAddress( false );
			DefaultIoFilterChainBuilder chain = acceptor.getFilterChain() ;
			chain.clear();
			chain.addLast ( "head", filter ) ;
			chain.addLast ( "codec", new ProtocolCodecFilter ( new CodecFactory ( ) ) ) ;
			acceptor.setHandler ( hander ) ;
			acceptor.bind ( new InetSocketAddress ( ContextLoader.SERVER_PORT () ) ) ;
			logger.info ( "server startup end ... " ) ;
		} catch ( Exception exc ) {
			logger.error ( exc.getMessage (), exc ) ;
		}
	}	
	
	public String logger () {
		return new StringBuilder ().append ( "server is running[" ).
				append ( acceptor.getManagedSessionCount () ).
				append ( "]" ).toString () ;		
	}
	
	public void dispose () {
		if ( acceptor != null ) {
			acceptor.unbind () ;
			acceptor.dispose () ;
			acceptor = null ;
		}
	}
	
	public boolean isClose () throws Exception {
		try {  
			_sync.acquire ();
			return _flag ;
		} catch ( Exception exc ) {  
			throw exc;  
		} finally {  
			_sync.release () ;  
		}
	}	
	
	public void close () throws Exception {
		try {  
			_sync.acquire ();
			_flag = true ;
		} catch ( Exception exc ) {  
			throw exc;  
		} finally {  
			_sync.release () ;  
		}
	}	
}